package yun.web.springmvc.ext;

import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Configuration;
import org.yaml.snakeyaml.Yaml;

@Configuration
public class MvcPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    //JSONObject jsonObject = new Yaml().loadAs(new ClassPathResource("app.yml").readUtf8Str(), JSONObject.class);
    JSONObject jsonObject = new Yaml().loadAs(new ClassPathResource("application.yml").getStream(), JSONObject.class);

    //Object eval = JSONPath.eval(jsonObject, "my.servers");
    public MvcPropertyPlaceholderConfigurer() {
        super.setPlaceholderPrefix("#(");
        super.setPlaceholderSuffix(")");
    }

    @Override
    protected String resolveSystemProperty(String key) {
        try {
            return JSONPath.eval(jsonObject, key).toString();
        } catch (Exception e) {
            return null;
        }
    }
}
